-- UITreeHouseUpgrade
-- Created by cheny3 Feb/22/2016
-- 树屋技能升级

UITreeHouseUpgrade = class("UITreeHouseUpgrade", function()
    return cc.Layer:create();
end);

function UITreeHouseUpgrade.create(skillId, skillLevel)
    return UITreeHouseUpgrade.new(skillId, skillLevel);
end

function UITreeHouseUpgrade:ctor(skillId, skillLevel)
    -- 初始化
    self:setName("UITreeHouseUpgrade");
    -- 复用魔藤技能界面
    local node = cc.CSLoader:createNode("layout/bine/BineSkillLearn.csb");
    self:addChild(node);
    self.node = node;

    self:setLocalZOrder(UIMgr.TOP_MENU_ZORDER + 1);

    self.skillId = skillId;
    self.skillLevel = skillLevel;

    -- 注册事件处理回调函数
    self:registerEventCallback()

    -- 注册点击事件
    self:registerTouchEvent();

    -- 重绘
    self:redraw();

    -- 适配
    self:resize();
end

-- 适配
function UITreeHouseUpgrade:resize()
    local node = self.node;

    -- bg居中显示并充满整个屏幕
    AlignM.fitToScreen(node, "bg");

    -- CT居中并缩放至合适大小
    AlignM.alignToCenter(node, "CT");

    -- BT置于底部并缩放至合适大小
    AlignM.alignToBottomCenter(node, "BT");
end

-- 注册事件处理回调函数
function UITreeHouseUpgrade:registerEventCallback()
    -- 关注升级技能事件
    EventMgr.register("UITreeHouseUpgrade", event.UPGRADE_TREE_HOUSE_SKILL, function(args)
        -- TODO
    end);

    self:registerScriptHandler(function(eventType)
        if eventType == "exit" then
            cclog("UITreeHouseUpgrade 界面析构清理");

            -- 移除所有事件关注
            EventMgr.removeAll("UITreeHouseUpgrade");
        end
    end);
end

-- 注册点击事件
function UITreeHouseUpgrade:registerTouchEvent()
    -- 注册返回按钮点击事件
    local btnBack = findChildByName(self.node, "BT/btn_back");
    local function onBackClick(sender, eventType)
        if eventType == ccui.TouchEventType.ended then
            AudioM.playFx("button_return");
            -- 关闭当前界面
            UIMgr.getCurrentScene():removeFormByName("UITreeHouseUpgrade");
        end
    end
    AddBackEventHandler(self, btnBack, onBackClick);
    TextStyleM.setTextStyle(btnBack, TextStyleM.TEXT_SIZE_MIDDLE, TextStyleM.TEXT_COLOR_BROWN_YELLOW, true);
    btnBack:setTitleText(getLocStr("btn_text_back"));

    -- 注册确定按钮点击事件
    local btnOk = findChildByName(self.node, "CT/btn_ok");
    local function onOkClick(sender, eventType)
        if eventType == ccui.TouchEventType.ended then
            AudioM.playFx("button_select");
            -- 尝试学习
            self:tryUpgradeSkill();
        end
    end
    btnOk:addTouchEventListener(onOkClick);
    TextStyleM.setTextStyle(btnOk, TextStyleM.TEXT_SIZE_MIDDLE, TextStyleM.TEXT_COLOR_BROWN_YELLOW, true);
    btnOk:setTitleText(getLocStr("btn_text_submit"));
end

-- 重绘
function UITreeHouseUpgrade:redraw()
    -- 技能名称
    local nameLabel = findChildByName(self.node, "CT/name");
    local itemId = TreeHouseM.querySkill(self.skillId, "class_id");
    local skillName = ItemM.query(itemId, "name");
    TextStyleM.setTextStyle(nameLabel, TextStyleM.TEXT_SIZE_MIDDLE, TextStyleM.TEXT_COLOR_DARK_RED);
    nameLabel:setString(skillName);

    -- 物品描述
    local classId = TreeHouseM.querySkill(self.skillId, "class_id");
    local itemDesc = ItemM.query(classId, "desc");

    -- 技能描述
    local skillDesc = TreeHouseM.querySkill(self.skillId, "skill_desc");
    local skillArgs = FormulaM.invoke("CALC_TREE_HOUSE_PROPERTY_ARGS",
        ME.user.tree_house, self.skillId, self.skillLevel, true);
    local attackValue = FormulaM.invoke("CALC_TREE_HOUSE_ITEM_ATTACK", skillArgs);

    -- 拼接描述
    local desc = itemDesc .. "\n\n" .. skillDesc;

    -- 计算伤害值
    desc = string.format(desc, attackValue);
    local descLabel = findChildByName(self.node, "CT/desc");
    TextStyleM.setTextStyle(descLabel, TextStyleM.TEXT_SIZE_SMALL, TextStyleM.TEXT_COLOR_DARK_BROWN);
    descLabel = applyString(descLabel, desc);

    -- 自适应所有组件
    local bg = findChildByName(self.node, "CT/bg");
    local btnOk = findChildByName(self.node, "CT/btn_ok");
    AlignM.adaptLayout(bg, AlignM.BG_SIZE_NORMAL, { nameLabel, descLabel, btnOk});
end

-- 尝试学习技能
function UITreeHouseUpgrade:tryUpgradeSkill()
    local ret = TreeHouseM.canUpgradeSkill(self.skillId);
    if ret ~= true then
        if ret == "cost_lack" then
            -- 消耗不足
            local cost    = TreeHouseM.calcUpgradeSkillCost(self.skillId);
            local classId = table.keys(cost)[1];
            local name    = ItemM.query(classId, "name");

            alert(string.format(getLocStr("item_not_enough"), name));
        elseif ret == "level_max" then
            -- 满级
            alert(getLocStr("tree_house_max_level"));
        end

        return;
    end

    -- 学习技能
    DungeonActionM.go("upgrade_tree_house_skill", self.skillId);

    -- 移除自身
    UIMgr.getCurrentScene():removeFormByName("UITreeHouseUpgrade");
end

